﻿using System;
using System.Web;
using System.Web.UI;
using StampController.BL;
using StampController.BL.Models;

namespace StampController
{
    public partial class SiteMaster : MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!HttpContext.Current.User.Identity.IsAuthenticated)
                {
                    NavigationMenu.Items.Clear();
                    if (HttpContext.Current.User.Identity.AuthenticationType == "Forms")
                    {
                        if (Request.Url.AbsolutePath != "/Account/Login.aspx")
                            Response.Redirect("/Account/Login.aspx");
                    }
                    else
                    {
                        if (Request.Url.AbsolutePath != "/")
                            Response.Redirect("/");
                    }
                    return;
                }
                var user = SessionUtils.Get<UsersModel>("USER");

                var authorizedPerson = GetUser(user.UserID);

                if (!(user.IsHOD | user.IsGD | authorizedPerson.IsHOD))
                {
                    if (Request.Url.AbsolutePath.EndsWith("/Pages/", StringComparison.OrdinalIgnoreCase) ||
                        Request.Url.AbsolutePath.EndsWith("ApprovalRequestForm.aspx", StringComparison.OrdinalIgnoreCase) ||
                        ((Request.Url.AbsolutePath.EndsWith("AuthorizationList.aspx", StringComparison.OrdinalIgnoreCase) ||
                        Request.Url.AbsolutePath.EndsWith("AuthorizerForm.aspx", StringComparison.OrdinalIgnoreCase)) &&
                        !(user.IsAdminOfficer | user.IsAdminSite)))
                        Response.Redirect("/");
                    NavigationMenu.Items.Remove(NavigationMenu.FindItem("Approval"));
                    if (!(user.IsAdminOfficer | user.IsAdminSite))
                    {
                        NavigationMenu.Items.Remove(NavigationMenu.FindItem("Delegation"));
                    }
                }
                if (!(user.IsAdminOfficer | user.IsAdminSite | authorizedPerson.IsAdminSite))
                {
                    if (Request.Url.AbsolutePath.EndsWith("PendingConfirmList.aspx", StringComparison.OrdinalIgnoreCase) ||
                        Request.Url.AbsolutePath.EndsWith("AdminConfirmForm.aspx", StringComparison.OrdinalIgnoreCase))
                        Response.Redirect("/");
                    NavigationMenu.Items.Remove(NavigationMenu.FindItem("Admin Officer"));
                }
                if (!user.IsAdminSite)
                {
                    if (Request.Url.AbsolutePath.EndsWith("ViewFeedback.aspx", StringComparison.OrdinalIgnoreCase))
                        Response.Redirect("/");
                    NavigationMenu.Items.Remove(NavigationMenu.FindItem("View Feedback"));
                }
            }
        }

        private static UsersModel GetUser(string userId)
        {
            var imAuthorized = new AuthorizedHistoryImpl();
            return imAuthorized.GetAuthorizedPerson(userId, DateTime.Now);
        }
    }
}